package com.hzhq.contract.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author hzhq
 * @create 2021/3/16 8:22 下午
 * @email hzhq1255@163.com
 */
public class EncryptUtil {

    public static String md5Encypt(String unencrypted){
        String encrypted = null;

        return encrypted;
    }


    public static String md5File(InputStream inputStream){
        return null;
    }

    public static String getMd5File(InputStream is) throws NoSuchAlgorithmException, IOException {
        StringBuilder md5 = new StringBuilder();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] dataBytes = new byte[1024];

        int nread = 0;
        while ((nread = is.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        };
        byte[] mdbytes = md.digest();

        // convert the byte to hex format
        for (int i = 0; i < mdbytes.length; i++) {
            md5.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return md5.toString();
    }

}
